<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class manage extends admin{
	private $db;
	public $siteid;
	function __construct() {
		parent::__construct();
		pc_base::load_app_class('global');
		pc_base::load_sys_class('form','',1);
		pc_base::load_sys_class('format', '', 0);
		$this->db = pc_base::load_model('variable_model');
		$this->admin_db = pc_base::load_model('admin_model');
		$this->siteid = get_siteid();//获取当前站点
		$this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
		$model_setting=pc_base::load_model('site_setting_model');
		$this->variable_color= $model_setting->getValue("variable_color",$this->siteid);//获取颜色
		foreach ($this->variable_color as $v){
			$color[$v] = $v;
		}
		$this->color = $color;
	}
	
	public function init() {
		$where = 'status=1';
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$variable = $this->db->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->db->pages;
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=variable&c=manage&a=add\', title:\''.'添加变量'.'\', width:\'580\', height:\'420\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', "添加变量");
		include $this->admin_tpl('variable_list');
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$data = isset($_POST['info']) ? $_POST['info'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			if(empty($data['name']) || empty($data['value'])){
				showmessage(L('illegal_parameters'),HTTP_REFERER);
			}
			$data['sort'] = $data['sort'] ? $data['sort'] : 0;
			$value = array_filter(explode(',', $data['value']));
			$sql = "";
			$sql .= "INSERT INTO nsrweb_variable_nsr (name,value,description,listorder,create_time,update_time,created_by,updated_by,status) VALUES ";
			foreach ($value as $k=>$v){
				$sql .= "('".$data['name']."','".$v."','".$data['description']."','".$data['listorder']."','".date('Y-m-d H:i:s',time())."','".date('Y-m-d H:i:s',time())."',".$this->admin_userid.",".$this->admin_userid.",1),";
			}
			$sql = trim($sql,',');
			$result = $this->db->query($sql);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">添加变量配置（APP）</span>[变量名称：'.$data['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('add_success'),'','','add');
			} else {
				showmessage(L('add_error'),HTTP_REFERER);
			}
		} else {
			include $this->admin_tpl('variable_add');
		}
	}
	
	public function edit() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message_not_exits'),HTTP_REFERER);
		if ($_POST['dosubmit']) {
			$data = isset($_POST['info']) ? $_POST['info'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$data['update_time'] = date('Y-m-d H:i:s',time());
			$data['updated_by'] = $this->admin_userid;
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">修改变量配置（APP）</span>[变量ID：'.$id.'，变量名称：'.$data['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('edit_success'),'','','edit');
			} else {
				showmessage(L('edit_error'),HTTP_REFERER);
			}
		} else {
			$info = $this->db->get_one(array('id'=>$id));
			include $this->admin_tpl('variable_edit');
		}
	}
	
	public function del(){
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message not exits'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$where = "id in ('$id')";
		$talent_name = $this->db->select($where,'name','','id DESC');
		foreach ($talent_name as $v){
			$name[] = $v['name'];
		}
		$result = $this->db->update(array('status'=>2),"id in ('$id')");
		if ($result) {
			$del_id = explode('\',\'', $id);
			$new_id = implode('，', $del_id);
			//记录行为日志
			$log_content = '<span style="color:red;">删除变量（APP）</span>[变量id：'.$new_id.',变量名称：'.json_encode($name,JSON_UNESCAPED_UNICODE).']';
			$this->manage_admin_log($log_content);
			showmessage(L('del_success'),'?m=variable&c=manage&a=init');
		} else {
			showmessage(L('del_error'),HTTP_REFERER);
		}
	}
	public function listorder(){
		if (isset($_GET['dosubmit'])) {
			$listorder = isset($_POST['listorders']) ? $_POST['listorders'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			foreach ($listorder as $id=>$order){
				$this->db->update(array('listorder'=>$order),array('id'=>$id));
			}
			$log_content = '<span style="color:#ffa500;">【标签配置】排序</span>';
			$this->manage_admin_log($log_content);
			showmessage(L('listorder_success'),'?m=variable&c=manage&a=init');
		} else {
			showmessage(L('listorder_error'),HTTP_REFERER);
		}
	}
}